package com.pocketsweet.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.ui.uilib.FlowLayout;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.RandomColorTagView;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.adapter.VoiceAdapter;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_edit_voice)
public class EditVoice extends BaseActivity implements OnClickListener {
	private HeaderLayout header;
	private VoiceAdapter voiceAdapter;
	private LoadingDailog loading;
	private String[] stringLanguage = { "亲昵爱称", "叫你起床", "给你唱歌", "逗你开心", "撒娇卖萌",
			"磁性暖男" };

	@ViewInject(R.id.RecommandTag)
	private static FlowLayout RecommandTag;

	@ViewInject(R.id.voiceListView)
	private static ListView voiceListView;

	@ViewInject(R.id.linVoice)
	private static LinearLayout linVoice;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initView();
		initTag();
		setHeaderFunction();
	}

	private BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Main.ACTION_VOICE)) {
				voiceAdapter.notifyDataSetChanged();
				voiceListView.setAdapter(voiceAdapter);
				if (Main.voiceList.size() > 0) {
					linVoice.setVisibility(View.VISIBLE);
				} else {
					linVoice.setVisibility(View.GONE);
				}
			}
		}
	};

	@Override
	public void onPause() {
		super.onPause();
		if (Main.MediaTimer != null && Main.mMediaPlayer.isPlaying()) {
			Main.mMediaPlayer.pause();
			Main.mMediaPlayer.stop();
			Main.mMediaPlayer.reset();
			Main.MediaTimer.cancel();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);

	}

	@Override
	protected void onStart() {
		super.onStart();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Main.ACTION_VOICE);
		registerReceiver(receiver, filter);

	}

	@Override
	protected void onResume() {
		super.onResume();
		voiceListView.setAdapter(voiceAdapter);
	}

	private void initView() {
		if (Main.voiceList != null &&Main.voiceList.size()>0) {
			// Main.voiceList.clear();
			linVoice.setVisibility(View.VISIBLE);
		}else{
			linVoice.setVisibility(View.GONE);
		}
		// Log.e("==voice", Main.voiceList.toString() + " ");
		voiceAdapter = new VoiceAdapter(EditVoice.this, Main.voiceList, true);
		voiceAdapter.notifyDataSetChanged();
		voiceListView.setAdapter(voiceAdapter);
		voiceListView.setCacheColorHint(0); 
	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setMiddleText("声线");
		header.setRightText("");
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				EditVoice.this.finish();
			}
		});
	}

	private void initTag() {
		for (int i = 0; i < stringLanguage.length; i++) {
			final RandomColorTagView itemView = new RandomColorTagView(
					EditVoice.this);
			itemView.setText(stringLanguage[i]);
			final String tab = stringLanguage[i];
			itemView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					String temp;
					if (Main.voiceList.size() > 0) {
						// for (int i = 0; i < Main.voiceList.size(); i++) {
						// temp = Main.voiceList.get(i).getTheme();
						// if (temp.equals(tab)) {
						ToolKits.toast(EditVoice.this, "你已有声线");
						return;
						// }
						// }
					}
					Intent recordintent = new Intent(EditVoice.this,
							DialogAudio.class);
					recordintent.putExtra("theme", tab);
					EditVoice.this.startActivity(recordintent);
				}
			});
			RecommandTag.addView(itemView);
		}
	}

	@Override
	public void onClick(View arg0) {

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}